const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 用于判断空，Undefined String Array Object
 */
function isBlank(str) {
  if (null == str) {//null
    return true
  }
  if (Object.prototype.toString.call(str) === '[object Undefined]') {//空
    return true
  } else if (
    Object.prototype.toString.call(str) === '[object String]' ||
    Object.prototype.toString.call(str) === '[object Array]') { //字条串或数组
    return str.length == 0 ? true : false
  } else if (Object.prototype.toString.call(str) === '[object Object]') {
    return JSON.stringify(str) == '{}' ? true : false
  } else {
    return str.length == 0 ? true : false
  }
}

/**
 * 隐藏字符串中关键code ,隐藏字符默认为'*'
 * 例如隐藏手机号，卡号：1890000000 - 189****0000; {{str|hide_code(3,4,'*')}}
 * @param str
 * @param pos 开始位置
 * @param length 替换字符个数
 * @param newChar 替换的字符/字符串
 * @returns {*}
 */
function hideStr(str, pos, length, newChar) {
  pos = pos || 0;
  length = length || 0;
  newChar = newChar || '*';
  if (pos < 0 || length <= 0 || pos + length > str.length) {
    return str;
  }
  var repStr = "";
  for (var i = 0; i < length; i++) {
    repStr += newChar;
  }
  return str.slice(0, pos) + repStr + str.slice(pos + length, str.length);
}

/**
 * 连接的字符串
 * strArray:字符串数组
 * conStr：连接的字符
 */
function concatStr(strArray, conStr) {
  var strLength = 1;
  var result = '';
  for (let obj of strArray) {
    result += obj;
    if (strLength < strArray.length) {
      result += conStr;
    }
    strLength++;
  }
  return result;
}

/**
 * 格式化日期
 */
function format(time, fmt) {
  time = time instanceof Date ? time : new Date(time);
  var o = {
    "M+": time.getMonth() + 1,                 //月份 
    "d+": time.getDate(),                    //日 
    "h+": time.getHours(),                   //小时 
    "m+": time.getMinutes(),                 //分 
    "s+": time.getSeconds(),                 //秒 
    "q+": Math.floor((time.getMonth() + 3) / 3), //季度 
    "S": time.getMilliseconds()             //毫秒 
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}

/**
 * 格式化日期 - （人性化）(附加时间)
 * @param {Number|Date} time
 * @return {string}
 */
function formatSmartTime(time) {
  time = time instanceof Date ? time.getTime() : time;
  var diffTime = new Date().getTime() - time + 20000;

  //今天凌晨时间戳
  const toDayTime = new Date().setHours(0, 0, 0);
  //昨天凌晨时间戳
  const yesterDayTime = toDayTime - 86400000;
  //明天凌晨时间戳
  const tomorrowTime = toDayTime + 86400000;
  //前天凌晨时间戳
  const beforeYesterdayTime = yesterDayTime - 86400000;
  //后天凌晨时间戳
  const afterTomorrowTime = tomorrowTime + 86400000;

  if (diffTime < 0) {
    diffTime = Math.abs(diffTime);
    //大于一分钟
    if (diffTime < 60000) return "一会儿";
    //大于一分钟小于一小时
    if (diffTime >= 60000 && diffTime < 3600000) return parseInt(diffTime / 60000) + "分钟后";
    //今天
    if (time < tomorrowTime) return "今天" + format(time, "hh:mm");
    //明天
    if (time < afterTomorrowTime) return "明天" + format(time, "hh:mm");
    //后天
    if (time < afterTomorrowTime + 86400000) return "后天" + format(time, "hh:mm");
  } else {
    //小于一分钟
    if (diffTime < 60000) return "刚刚";
    //大于一分钟小于一小时
    if (diffTime >= 60000 && diffTime < 3600000) return parseInt(diffTime / 60000) + "分钟前";
    //今天
    if (time > toDayTime) return "今天" + format(time, "hh:mm");
    //昨天
    if (time > yesterDayTime) return "昨天" + format(time, "hh:mm");
    //前天
    if (time > beforeYesterdayTime) return "前天" + format(time, "hh:mm");
  }
  //月份/日 大于今年开始时间
  const toYearTime = new Date();
  toYearTime.setMonth(0, 0);
  toYearTime.setHours(0, 0, 0, 0);
  const toYearTime2 = new Date(time);
  toYearTime2.setMonth(0, 0);
  toYearTime2.setHours(0, 0, 0, 0);
  if (toYearTime.getTime() == toYearTime2.getTime())
    return format(time, "M月d日 hh:mm");
  return format(time, "yyyy年M月d日 hh:mm");
}
/**
 * 验证中国手机号码的有效性
 */
function isMobile(mobile, title = null) {
  if (mobile.length == 0) {
    //return this.showToastError('请输入您的手机号！')
    return false;
  }
  if (mobile.length != 11) {
    //return this.showToastError('请检查您的手机号长度！')
    return false;
  }
  let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
  if (!reg.test(mobile)) {
    //return this.showToastError('手机号有误，请重新检查！')
    return false;
  }
  return true
}

/**
 * 验证密码是否正确
 */
function isPasword(password, title = null) {
  let reg = /^[A-Za-z0-9]{6,20}$/;
  if (!reg.test(password)) {
    return false;
  }
  return true
}

/**
 * 判断版本号
 */
function compareVersion(v1, v2) {
  v1 = v1.split('.')
  v2 = v2.split('.')
  var len = Math.max(v1.length, v2.length)

  while (v1.length < len) {
    v1.push('0')
  }
  while (v2.length < len) {
    v2.push('0')
  }

  for (var i = 0; i < len; i++) {
    var num1 = parseInt(v1[i])
    var num2 = parseInt(v2[i])

    if (num1 > num2) {
      return 1
    } else if (num1 < num2) {
      return -1
    }
  }

  return 0
}

module.exports = {
  formatTime: formatTime,
  isBlank: isBlank,
  hideStr: hideStr,
  concatStr: concatStr,
  format: format,
  formatSmartTime: formatSmartTime,
  isMobile: isMobile,
  compareVersion:compareVersion,
  isPasword: isPasword
}
